package com.heima.article.listener;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.heima.article.service.ArticleConfigService;
import com.heima.model.article.pojo.ApArticleConfig;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.function.Consumer;

/**
 * 文章上下架监听器
 */
@Component
public class UpOrDownListener {

    @Autowired
    private ArticleConfigService articleConfigService;
    @KafkaListener(topics = "upOrDown")
    public void receiveMessage(ConsumerRecord<String,String> message){
        //获取参数
        String articleId = message.key();
        String enable = message.value();
        //修改状态
        ApArticleConfig articleConfig = new ApArticleConfig();
        if (Integer.parseInt(enable)  == 1){
            articleConfig.setIsDown(false);
        }else {
            articleConfig.setIsDown(true);
        }
        articleConfig.setArticleId(Long.valueOf(articleId));
        LambdaUpdateWrapper<ApArticleConfig> updateWrap = new LambdaUpdateWrapper();
        updateWrap.set(ApArticleConfig::getIsDown,"0".equals(enable));
        updateWrap.eq(ApArticleConfig ::getArticleId,articleId);
        articleConfigService.update(updateWrap);
    }
}
